﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace VBAConvertor.Parsers
{
    public class InWithParser : ILineParser
    {
        private static readonly Regex InWithPattern =
            new Regex(
                @"^\.[a-zA-Z]+",
                RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.Singleline);

        public string Parse(Stack<ParseStatus> statuses, string lineContent, LinesReader reader, out bool success, ref bool lineClosed)
        {
            var match = InWithPattern.Match(lineContent);
            success = match.Success;
            if (!success) return lineContent;

            if (statuses.Peek().CurrentState != State.WithDef)
            {
                throw new Exception();
            }

            return InWithPattern.Replace(lineContent, m => statuses.Peek().StateInfo.ToString() + m.Value);
        }
    }
}
